#include<iostream>
using namespace std;
const int N =1010;
int f[N];
int t[N], v[N];
int T, M;
int main()
{
	cin >> T >> M;
	for (int i = 1;i <= M;i++)
	{
		cin >> t[i] >> v[i];
	}
	
	for (int i = 1;i <= M;i++)
	{
		for (int j=T;j>=t[i];j--)
		{
			f[j] = max(f[j],f[j - t[i]] + v[i]);
		}
	}
	cout << f[T] << endl;
	return 0;
}
